<?php
/**
 * Waf
 *
 * @author		黑冰 <954650386@qq.com>
 */

namespace waf;

class Waf
{
    private static $_config;
    private static $_objServer;
    private static $_arrWData;

    //==================== 差异化合并多个数组
    // array_merge 会把相同的key做更新，如果合并的是多级数组，不能做到差异化合并
    public static function array_merge_diff()
    {
        $arrR   = array();
        $intArg = func_num_args();

        // 参数必须大于2
        if($intArg >= 2)
        {
            $arrR = func_get_arg(0);
            for($i = 1; $i < $intArg; $i++)
            {
                $arrT = func_get_arg($i);
                if($arrT && is_array($arrT))
                {
                    self::do_array_merge_diff($arrR, $arrT);
                }
            }
        }

        return $arrR;
    }

    public static function do_array_merge_diff(&$to, $from)
    {
        foreach($from as $k => $v)
        {
            if(is_array($v))
            {
                self::do_array_merge_diff($to[$k], $v);
            }else{
                $to[$k] = $v;
            }
        }
    }

    //==================== 配置相关
    public static function initConfig($conf)
    {
        if(self::$_config == null)
            self::$_config = $conf;
    }

    public static function addConfig($k1, $k2, $v = '')
    {
        self::$_config[$k1][$k2] = $v;
    }

    public static function getConfig($k1, $k2 = null)
    {
        $strR = '';
        
        if($k1)
        {
            if($k2)
            {
                if(isset(self::$_config[$k1][$k2]))
                    $strR = self::$_config[$k1][$k2];
            }else{
                $strR = self::$_config[$k1];
            }
        }
        
        return $strR;
    }

    public static function getConfigMap()
    {
        return self::$_config;    
    }

    //==================== swoole server objServer 相关
    public static function initObjServer($objServer)
    {
        if(self::$_objServer == null)
            self::$_objServer = $objServer;
    }

    public static function getObjServer()
    {
        return self::$_objServer;
    }

    //==================== waf data
    public static function WData()
    {
        if(self::$_arrWData == null)
        {
            self::$_arrWData = new WafData();
        }

        return self::$_arrWData;
    }
}

class WafData
{
    private $_arrWData  = array();

    public function setData($k = '', $v = '')
    {
        $this->_arrWData[$k] = $v;
    }

    public function getData($k = '')
    {
        return $this->_arrWData[$k];
    }

    public function getAllData()
    {
        return $this->_arrWData;
    }

    public function delData($k = '')
    {
        unset($this->_arrWData[$k]);
    }
}
?>
